<template>
	<div class="user-pwd">
		<el-page-header @back="goBack" content="修改密码"></el-page-header>
		<el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
			<el-form-item label="旧密码" prop="old_password">
				<el-input type="password" v-model="ruleForm.old_password" show-password placeholder="请输入旧密码" autocomplete="off" ref="old_password"></el-input>
			</el-form-item>
			<el-form-item label="新密码" prop="password">
				<el-input type="password" v-model="ruleForm.password" show-password placeholder="请输入新密码，长度至少为6位" autocomplete="off"></el-input>
			</el-form-item>
			<el-form-item label="确认密码" prop="password2">
				<el-input type="password" v-model="ruleForm.password2" show-password placeholder="确认密码" autocomplete="off"></el-input>
			</el-form-item>
			<el-form-item>
				<el-button v-no-more-click:3000 @click="resetForm('ruleForm')">重置</el-button>
				<el-button v-no-more-click:3000 type="primary" @click="submitForm('ruleForm')">提交</el-button>
			</el-form-item>
		</el-form>
	</div>
</template>
<script>
	export default {
		name: "updatePwd",
		data() {
			let that = this;
			var validatePass2 = function(rule, value, callback) {
				if (value === '') {
					callback(new Error('请填写新密码'));
				}else if (value !== that.ruleForm.password) {
					callback(new Error('两次密码不一致'));
				}else {
					callback();
				}
			};
			return {
				ruleForm: {
					old_password:'',
					password:'',
					password2:''
				},
				rules: {
					old_password: [{
						required: true,
						message: "请输入旧密码",
						trigger: "blur"
					},
					{
						min: 6,
						max: 12,
						message: "密码长度为6-12位",
						trigger: "blur"
					}],
					password: [{
							required: true,
							message: "请输入新密码",
							trigger: "blur"
						},
						{
							min: 6,
							max: 12,
							message: "密码长度为6-12位",
							trigger: "blur"
						}
					],
					password2: [{
							required: true,
							validator: validatePass2,
							trigger: "blur"
						},
						{
							min: 6,
							max: 12,
							message: "密码长度为6-12位",
							trigger: "blur"
						}
					]
				}
			};
		},
		methods: {
			goBack() {
				this.$router.go(-1);
			},
			submitForm(formName) {
				this.$refs[formName].validate(valid => {
					if (valid) {
						this.$axios
							.put(
								"/api/consumer/pwd", {
									old_password: this.ruleForm.old_password,
									password: this.ruleForm.password
								}
							)
							.then(res => {
								if (res.data.code == 400) {
									this.$message.error(res.data.message);
									//原密码错误，清空输入并聚焦
									this.ruleForm.old_password = '';
									this.changOldPwdfouce();
								}
								if (res.data.code == 200) {
									this.$message.success("密码修改成功");
									//this.$store.commit("delUser");
									this.$cookies.remove('user');
									this.$cookies.remove('token');
									this.$store.commit("delUser");
									this.$axios.defaults.headers.common["Authorization"] = "";
									this.$router.push({
										path: "/"
									})
								}
							})
					} else {
						this.$message.error("请按要求填写密码");
						return false;
					}
				});
			},
			resetForm(formName) {
				this.$refs[formName].resetFields();
			},
			changOldPwdfouce() {
				this.$nextTick(x => {
					//正确写法
					this.$refs.old_password.focus();
				});
			}
		}
	};
</script>
<style lang="less">
	.user-pwd {
		.el-form {
			width: 800px;
			margin: 80px 0 0 50px;

			.el-input {
				width: 300px;
			}
		}
	}
</style>
